﻿/// <summary>
/// 比较类型
/// </summary>
public enum CompareType
{
    Equal,
    NotEqual,
    Greater,
    GEqual,
    Less,
    LEqual
}


public class DBParam
{
    public string key;
    public object value;
    public CompareType type;

    public DBParam(string key, object value, CompareType type)
    {
        this.key = key;
        this.value = value;
        this.type = type;
    }

    public bool IsSatisfy(object obj)
    {
        if (value is string) return _CompareString(obj.ToString());

        if (value is int) return _CompareInt(int.Parse(obj.ToString()));

        if (value is float) return _CompareFloat(float.Parse(obj.ToString()));

        return false;
    }

    private bool _CompareString(string str)
    {
        if (str == null) return false;
        
        if (type == CompareType.Equal)
            return value.ToString().Equals(str);
        else if (type == CompareType.NotEqual)
            return !value.ToString().Equals(str);
        
        return false;
    }

    private bool _CompareInt(int num)
    {
        int valueInt = int.Parse(value.ToString());
        switch (type)
        {
            case CompareType.Equal:     return num == valueInt;
            case CompareType.NotEqual:  return num != valueInt;
            case CompareType.Less:      return num < valueInt;
            case CompareType.LEqual:    return num <= valueInt;
            case CompareType.Greater:   return num > valueInt;
            case CompareType.GEqual:    return num >= valueInt;
        }

        return false;
    }

    private bool _CompareFloat(float num)
    {
        float valueInt = int.Parse(value.ToString());
        switch (type)
        {
            case CompareType.Equal:     return num == valueInt;
            case CompareType.NotEqual:  return num != valueInt;
            case CompareType.Less:      return num < valueInt;
            case CompareType.LEqual:    return num <= valueInt;
            case CompareType.Greater:   return num > valueInt;
            case CompareType.GEqual:    return num >= valueInt;
        }
        return false;
    }

}
